package com.wangbo.restTemplate.antiepidemic;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSONObject;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.core.io.FileSystemResource;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.client.ClientHttpResponse;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.DefaultResponseErrorHandler;
import org.springframework.web.client.RestTemplate;

import java.io.File;
import java.util.ArrayList;
import java.util.Arrays;

public class TestAntiepidemic {
    public static void main(String[] args) {
        RestTemplate restTemplate = new RestTemplateBuilder().errorHandler(new DefaultResponseErrorHandler(){
            /**
             * 设置RestTemplate自带的异常处理器不做异常处理
             **/
            @Override
            public void handleError(ClientHttpResponse response) {
            }
        }).build();

        // 批量注册
        File dir = new File("C:\\Users\\Administrator\\Documents\\WeChat Files\\wxid_b82cqg8xkx1222\\FileStorage\\File\\2022-01\\沁园5");
        ArrayList<String[]> badData = new ArrayList<>();
        for (File file : dir.listFiles()) {
            String[] strings = test03(file);
            Long mid = test01(restTemplate, file, strings[1], strings[0]);
            if (-1 != mid) {
                test02(restTemplate, mid, strings[1], strings[0], "szlx", 11008L, strings[2]);
            } else {
                badData.add(strings);
            }
        }
        for (String[] strings : badData) {
            System.out.println(Arrays.toString(strings));
        }
    }

    /**
     * 人脸上传 (苏证通实人认证)
     */
    public static Long test01(RestTemplate restTemplate, File file, String name, String personalIdNo) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.MULTIPART_FORM_DATA);

        MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();
        param.add("multipartFile ",new FileSystemResource(file));
        param.add("name", name);
        param.add("personalIdNo", personalIdNo);

        HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(param, headers);
        JSONObject jsonObject = restTemplate.postForObject("http://58.221.238.109:8093/aitest/admin/apply/media/szt", httpEntity, JSONObject.class);
        System.out.println(jsonObject);
        JSONObject data = jsonObject.getJSONObject("data");
        return data.getLong("mid");
    }

    /**
     * 人员注册
     */
    public static void test02(RestTemplate restTemplate, Long mid, String name, String personalIdNo, String captcha, Long unitid, String username) {
        HttpHeaders headers = new HttpHeaders();
        headers.setContentType(MediaType.APPLICATION_JSON);

        JSONObject params = new JSONObject();
        params.put("captcha", captcha);
        params.put("idTypeId", 1001);
        params.put("mid", mid);
        params.put("name", name);
        params.put("personalIdNo", personalIdNo);
        params.put("unitid", unitid);
        params.put("username", username);

        HttpEntity<JSONObject> httpEntity = new HttpEntity<>(params, headers);
        JSONObject jsonObject = restTemplate.postForObject("http://58.221.238.109:8093/aitest/admin/apply/userRegister", httpEntity, JSONObject.class);
        System.out.println(jsonObject);
    }

    /**
     * 解析文件名
     */
    public static String[] test03(File file) {
        String union = StrUtil.subBefore(file.getName(), ".jpg", true);
        return union.split("_");
    }
}
